package com.smart.manager.member.repository;

import cn.hutool.core.lang.Opt;
import com.smart.common.entities.core.Page;
import com.smart.manager.member.entities.bodys.RMemberMaterialPigUse;
import com.smart.manager.member.entities.enums.PigUseKind;
import com.smart.manager.member.entities.enums.PigUseState;
import com.smart.manager.member.entities.models.MemberMaterialPigUse;
import com.smart.manager.member.entities.models.query.QMemberMaterialPigUse;
import com.smart.manager.member.entities.views.MemberPigView;
import com.smart.manager.member.entities.views.MemberView;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;


/**
 * 玩家猪猪 - 消费订单
 * @author leo
 * @date 2023/09/22
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class MemberMaterialPigUseRepository {


    public Page<MemberMaterialPigUse> pageList(RMemberMaterialPigUse.SearchBody search) {
        QMemberMaterialPigUse query = new QMemberMaterialPigUse();
        Opt.ofBlankAble(search.getPhoneNumber()).ifPresent(x -> query.member.phoneNumber.eq(x));
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        Opt.ofBlankAble(search.getKind()).ifPresent(x -> query.kind.eq(x));
        Opt.ofBlankAble(search.getWorkOrder()).ifPresent(x -> query.workOrder.contains(x));

        query.order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<MemberMaterialPigUse>().load(query.findPagedList());
    }

    /**
     * 创建消费
     * @param memberId 玩家id
     * @param pigId 猪仔id
     * @param kind 分类
     * @param state 状态
     * @param number 数量
     */
    public void add(Long memberId, Long pigId, PigUseKind kind, PigUseState state,Integer number,String order){

        new MemberMaterialPigUse().setMember(new MemberView().setId(memberId)).setPigId(new MemberPigView().setId(pigId)).setKind(kind).setStatus(state).setNumber(number).setWorkOrder(order).insert();

    }
}
